package dao.impl;

import dao.IFileDAO;
import pojo.FileItem;
import utils.JDBCUtils;

import java.sql.SQLException;
import java.util.List;

/**
 *文件数据访问类实现
 */

public class FileDAOImpl implements IFileDAO {
    @Override
    public List<FileItem> selectFileItemByUserId(Long userId) throws SQLException {
        return JDBCUtils.query(FileItem.class,"select * from t_file where user_id=?",userId);
    }

    @Override
    public FileItem selectFileItemByUserIdAndFilename(Long userId, String filename) throws SQLException {
        List<FileItem> list = JDBCUtils.query(FileItem.class,
                "select * from t_file where user_id=? and filename=? ", userId, filename);
        if(list.isEmpty()){
            return null;
        }
        return list.get(0);

    }

    @Override
    public FileItem selectFileItemById(Long id) throws SQLException {
        List<FileItem> list = JDBCUtils.query(FileItem.class,
                "select * from t_file where id=?", id);
        if(list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public void insertFileItem(FileItem fileItem) throws SQLException {
        JDBCUtils.update("insert into t_file(filename,path,length,user_id,open,create_time)" +
                "values(?,?,?,?,?,?)",fileItem.getFileName(),fileItem.getPath(),
                fileItem.getLength(),fileItem.getUser_id(),fileItem.getOpen(),fileItem.getCrate_time());
    }

    @Override
    public void updateFileItem(FileItem fileItem) throws SQLException {
        JDBCUtils.update("update  t_file set filename=?,path=?,length=?,user_id=?,open=?,create_time=? where id=?"
                       ,fileItem.getFileName(),fileItem.getPath(), fileItem.getLength(),
                fileItem.getUser_id(),fileItem.getOpen(),fileItem.getCrate_time(),fileItem.getUser_id());
    }

    @Override
    public void deleteFileItem(Long id) throws SQLException {
        JDBCUtils.update("delete from t_file where id=?"
               ,id);
    }
}
